package com.tj.collection.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.google.common.collect.Lists;
import com.tj.collection.bean.ApiResponse;
import com.tj.collection.common.RequestLocalInfo;
import com.tj.collection.controller.chatter.response.ChatterListResponse;
import com.tj.collection.db.domain.AdminChatterVo;
import com.tj.collection.db.domain.Role;
import com.tj.collection.db.enums.RoleKeyEnum;
import com.tj.collection.db.service.AdminRoleService;
import com.tj.collection.db.service.AdminService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class BaseCompent {

    @Resource
    AdminService adminService;
    @Resource
    AdminRoleService adminRoleService;

    protected AccountRole getAccountRole() {
        // 用户拥有的角色
        final List<Role> roles = adminRoleService.getRolesByAdminId(RequestLocalInfo.getCurrentAdmin().getId());
        if (CollectionUtils.isEmpty(roles)) {
            throw new ApplicationException("The current account has no access permission.");
        }

        // 当前用户是否为超管或具备查看所有数据权限
        final boolean hasViewAllDataPermission = roles.parallelStream().anyMatch(role -> RoleKeyEnum.SUPERADMIN.getType().equals(role.getRoleKey()) || RoleKeyEnum.VIEW_ALL_DATA_PERMISSION.getType().equals(role.getRoleKey()));
        if (hasViewAllDataPermission) {
            return new AccountRole(Boolean.TRUE, null);
        }

        List<String> chatterCodes = adminService.listAdminChatter(RequestLocalInfo.getCurrentAdmin().getId());
        if (CollectionUtils.isEmpty(chatterCodes)) {
            chatterCodes = Lists.newArrayList();
            chatterCodes.add("NoAccessPermission");
            return new AccountRole(Boolean.FALSE, chatterCodes);
        }

        return new AccountRole(Boolean.FALSE, chatterCodes);
    }

    protected AccountRole getAccountRoleByAdminId(Long adminId) {
        // 用户拥有的角色
        final List<Role> roles = adminRoleService.getRolesByAdminId(adminId);
        if (CollectionUtils.isEmpty(roles)) {
            throw new ApplicationException("The current account has no access permission.");
        }

        // 当前用户是否为超管或具备查看所有数据权限
        final boolean hasViewAllDataPermission = roles.parallelStream().anyMatch(role -> RoleKeyEnum.SUPERADMIN.getType().equals(role.getRoleKey()) || RoleKeyEnum.VIEW_ALL_DATA_PERMISSION.getType().equals(role.getRoleKey()));
        if (hasViewAllDataPermission) {
            return new AccountRole(Boolean.TRUE, null);
        }

        List<String> chatterCodes = adminService.listAdminChatter(adminId);
        if (CollectionUtils.isEmpty(chatterCodes)) {
            chatterCodes = Lists.newArrayList();
            chatterCodes.add("NoAccessPermission");
            return new AccountRole(Boolean.FALSE, chatterCodes);
        }

        return new AccountRole(Boolean.FALSE, chatterCodes);
    }
    protected List<String> gethatterCodes(List<Long> adminIds) {
        if (CollectionUtils.isEmpty(adminIds)) {
            return Lists.newArrayList();
        }
        final List<AdminChatterVo> adminChatterVos = adminService.listAllAdminChatter();
        if (CollectionUtils.isEmpty(adminChatterVos)) {
            return Lists.newArrayList();
        }
        return adminChatterVos.parallelStream().filter(o -> adminIds.contains(o.getAdminId())).map(AdminChatterVo::getChatterCode).collect(Collectors.toList());
    }

    protected Date toBeginDate(String date) {
        if (StringUtils.isEmpty(date)) {
            return DateUtil.beginOfDay(new Date());
        }
        final DateTime dateTime = DateUtil.parseDate(date);
        return DateUtil.beginOfDay(dateTime);
    }


    protected Date toEndDate(String date) {
        if (StringUtils.isEmpty(date)) {
            return DateUtil.endOfDay(new Date());
        }
        final DateTime dateTime = DateUtil.parseDate(date);
        return DateUtil.endOfDay(dateTime);
    }

    protected String convertBeginDate(String date) {
        if (StringUtils.isEmpty(date)) {
            return DateUtil.formatDateTime(DateUtil.beginOfDay(new Date()));
        }
        final DateTime dateTime = DateUtil.parseDate(date);
        return DateUtil.formatDateTime(DateUtil.beginOfDay(dateTime));
    }

    protected String convertEndDate(String date) {
        if (StringUtils.isEmpty(date)) {
            return DateUtil.formatDateTime(DateUtil.endOfDay(new Date()));
        }
        final DateTime dateTime = DateUtil.parseDate(date);
        return DateUtil.formatDateTime(DateUtil.endOfDay(dateTime));
    }

    public class AccountRole {
        private Boolean hasViewAllDataPermission;
        private List<String> chatterCodes;

        public AccountRole(Boolean hasViewAllDataPermission, List<String> chatterCodes) {
            this.hasViewAllDataPermission = hasViewAllDataPermission;
            this.chatterCodes = chatterCodes;
        }

        public Boolean getHasViewAllDataPermission() {
            return hasViewAllDataPermission;
        }

        public void setHasViewAllDataPermission(Boolean hasViewAllDataPermission) {
            this.hasViewAllDataPermission = hasViewAllDataPermission;
        }

        public List<String> getChatterCodes() {
            return chatterCodes;
        }

        public void setChatterCodes(List<String> chatterCodes) {
            this.chatterCodes = chatterCodes;
        }
    }
}
